package BinaryTree;

import com.sun.source.tree.Tree;

import java.util.LinkedList;
import java.util.Queue;

public class BinaryTree {
    //建立树节点
    static class TreeNode{
        char val;//树节点中存储的数据
        TreeNode left;//存储左孩子的引用
        TreeNode right;//存储右孩子的引用

        public TreeNode(char val){
            this.val=val;
        }

    }
    public TreeNode createTree(){
        TreeNode A=new TreeNode('A');
        TreeNode B=new TreeNode('B');
        TreeNode C=new TreeNode('C');
        TreeNode D=new TreeNode('D');
        TreeNode E=new TreeNode('E');
        TreeNode F=new TreeNode('F');
        TreeNode G=new TreeNode('G');
        TreeNode H=new TreeNode('H');
        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
        E.right=H;
        return A;
    }
    public TreeNode createTree2(){
        TreeNode A=new TreeNode('A');
        TreeNode B=new TreeNode('B');
        TreeNode C=new TreeNode('C');
        TreeNode D=new TreeNode('D');
        TreeNode E=new TreeNode('E');
        TreeNode F=new TreeNode('F');
        TreeNode G=new TreeNode('G');
        TreeNode H=new TreeNode('H');
        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        return A;
    }
    //前序遍历
    public void preOrder(TreeNode root){
        if(root==null){
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //中序遍历
    public void inOrder(TreeNode root){
        if(root==null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    //后序遍历
    public void postOrder(TreeNode root){
        if(root==null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }
    public static int nodeSize=0;
    // 获取树中节点的个数
    public int size(TreeNode root){
        if(root==null){
            return 0;
        }
        size(root.left);
        size(root.right);
        nodeSize++;
        return nodeSize;

    }
    public int size2(TreeNode root){
        if(root==null){
            return 0;
        }
        return size2(root.left)+size2(root.right)+1;

    }
    public static int leafSize=0;
    // 获取叶子节点的个数
    public int getLeafNodeCount(TreeNode root){
        if(root==null)
            return 0;
        if(root.left==null&&root.right==null) {
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
        return leafSize;
    }
    // 子问题思路-求叶子结点个数
    public int getLeafNodeCount2(TreeNode root){
        if(root==null)
            return 0;
        if(root.left==null&&root.right==null) {
            return 1;
        }
        return getLeafNodeCount2(root.left)+getLeafNodeCount2(root.right);
    }

    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root,int k){
        if(root==null){
            return 0;
        }
        if(k==1){
            return 1;
        }
        int leftNum=getKLevelNodeCount(root.left,k-1);
        int rightNum=getKLevelNodeCount(root.right,k-1);
        return leftNum+rightNum;
    }
    // 获取二叉树的高度
    public int getHeight(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftheight=getHeight(root.left);
        int rightheight=getHeight(root.right);
        return Math.max(leftheight,rightheight)+1;
    }
    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, int val){
        if(root==null){
            return null;
        }
        if(root.val==val){
            return root;
        }
        TreeNode leftT=find(root.left,val);
        TreeNode rightT=find(root.right,val);
        if(leftT!=null){
            return leftT;
        }
        if(rightT!=null){
            return rightT;
        }
        return null;
    }
    //层序遍历 可以用队列/数组实现
    public void levelOrder(TreeNode root){
        if(root==null)
            return;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        //队列中存放节点，当队列为空的时候意味着层序遍历结束
        //每遍历一个节点就打印一个 打印的时候让节点从队列中弹出
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null){
                queue.offer(cur.left);
            }
            if(cur.right!=null){
                queue.offer(cur.right);
            }
        }

    }
    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root){
    if(root==null){
        return true;
    }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            //完全二叉树和非完全二叉树的区别在于
            //放入队列中的节点如果出现null后，最后队列中只有null
            //而非完全二叉树在队列中出现null时，队列中还剩的有节点
            if(cur!=null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else{
                break;
            }
        }
        while (!queue.isEmpty()){
            //查看队列顶端元素
            TreeNode peek=queue.peek();
            if(peek!=null){
                return false;
            }
            queue.poll();
        }
    return true;
    }
  //判断两棵树是否相同
    //1.结构相同 2.节点相同 才是一棵相同的树
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //1.先判断结构
        if(p==null&&q==null){
            return true;
        }
        if(p!=null&&q==null||p==null&&q!=null){
            return false;
        }
        //2.再判断值
        if(p.val!= q.val){
            return false;
        }
        return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);

    }
    //判断是否是另一棵树的子树
    public boolean isSubtree(TreeNode root,TreeNode subRoot){
        if(root==null){
            return false;
        }
        //如果根节点相同
        if(isSameTree(root,subRoot)) return true;
        //递归root左子树
        if(isSubtree(root.left,subRoot)) return true;
        //递归root右子树
        if(isSubtree(root.right,subRoot)) return true;
        return false;
    }
    //翻转二叉树
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        TreeNode tmp=root.left;
        root.left=root.right;
        root.right=tmp;
        invertTree(root.left);
        invertTree(root.right);

        return root;
    }
}
